package com.ftg.midamall.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ftg.midamall.admin.dto.UmsRoleDto;
import com.ftg.midamall.admin.service.IUmsRoleService;
import com.ftg.midamall.admin.service.impl.UmsRoleServiceImpl;
import com.ftg.midamall.comm.vo.CodeStatusEnum;
import com.ftg.midamall.comm.vo.RespMessUtil;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author qjw
 * @since 2023-10-25
 */
@RestController
@Api(tags = "角色模块")
@RequestMapping("/ums/role/umsRole")
public class UmsRoleController {

    @Resource
    UmsRoleServiceImpl iumsRoleService;


    @GetMapping("/showAllWithPage")
    @ApiOperation("管理员查看角色信息")
    public RespMessUtil<IPage> showRole(
            @ApiParam(value = "页码", name = "pageSize", required = true) @RequestParam(value = "pageSize", defaultValue = "1") Long pageSize,
            @ApiParam(value = "显示条数", name = "pageLimit", required = true) @RequestParam(value = "pageLimit", defaultValue = "10") Long pageLimit,
            @ApiParam(value = "角色名称", name = "name") @RequestParam(value = "name", required = false) String roleName

    ) {

        return iumsRoleService.showAllWithPage(pageSize, pageLimit, roleName);
    }

    @PostMapping("/insertRole")
    @ApiOperation("管理员添加角色")
    public RespMessUtil<Integer> insertRole(@RequestBody UmsRoleDto ur, @RequestParam(value = "menuIds", required = false) List<Long> menuIds) {

        return iumsRoleService.insertRole(ur, menuIds);
    }

    @GetMapping("/selectMeunIds/{id}")
    @ApiOperation("获取角色对应的菜单id")
    public RespMessUtil<List<Integer>> selectMeunIds(
          @ApiParam(name = "id", value = "主键", required = true) @PathVariable("id")  Long id) {

        return iumsRoleService.selectMeunIds(id);
    }

    @PutMapping("/updateRole")
    @ApiOperation("管理员更改信息")
    public RespMessUtil<Integer> updateRole(@RequestBody UmsRoleDto ur, @RequestParam(value = "menuIds", required = false) List<Long> menuIds) {

        return iumsRoleService.updateById(ur, menuIds);
    }


    @DeleteMapping("/deleteRole/{id}")
    @ApiOperation("管理员删除角色")
    public RespMessUtil<Integer> delRole(
            @ApiParam(name = "id", value = "主键", required = true) @PathVariable("id") Long id) {

        Integer deleteRole = iumsRoleService.deleteById(id);

        return deleteRole > 0 ? RespMessUtil.build(CodeStatusEnum.SUCCESS, deleteRole) : RespMessUtil.deleteFailure();
    }


    @DeleteMapping("/deleteRoleByIDS")
    @ApiOperation("管理员批量删除角色")
    public RespMessUtil<Integer> delByIds(
            @ApiParam(name = "ids", value = "主键", required = true) @RequestParam("ids") List<Long> ids) {

        Integer ids1 = iumsRoleService.deleteBatchByIds(ids);

        return ids1 > 0 ? RespMessUtil.build(CodeStatusEnum.SUCCESS, ids1) : RespMessUtil.deleteFailure();
    }




    @ApiOperation("切换状态")
    @PutMapping("/switchStatus/{id}")
    public RespMessUtil<Boolean> updatestatus(
            @ApiParam(name = "id", value = "主键", required = true) @PathVariable("id") Long id) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, iumsRoleService.switchStatus(id));
    }


}
